<?php

namespace app\admin\controller\common;

use think\facade\Log;
use app\BaseController;

/**
 * 气象站被冻接受数据接口
 */
class Index extends BaseController
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 接受气象站数据接口
     *
     */
    public function index()
    {
        $param = $this->request->get();
        if (empty($param['date']) || empty($param['data'])) {
            Log::record($param,'notice');
            echo 'success1';
        }
        $dateTime = $param['date'];
        $dateTime = $this->getDate($dateTime);
        $thisTime = time();
        $hour = date('Y-m-d H',strtotime($dateTime));
        $data = explode(',',$param['data']);

        $addData = [
            'temperature'=>$data[0],
            'humidity'=>$data[1],
            'sunlight'=>$data[2],
            'co2'=>$data[3],
            'soil_temperature'=>$data[4],
            'soil_humidity'=>$data[5],
            'direction'=>$data[6],//风向
            'speed'=>$data[7],
            'rainfall'=>$data[8],
            'createtime'=>date('Y-m-d H:i:s',$thisTime),
            'createDate'=>date('Y-m-d',strtotime($dateTime)),
            'hour'=>$hour,
            'statisticstime'=>$dateTime
        ];
        db('z_weather_device_data')->insert($addData);
        $deviceStatistics = db('z_weather_device_data_statistics')->where('hour',$hour)->value('id');
        if (empty($deviceStatistics)) {
            db('z_weather_device_data_statistics')->insert($addData);
        }
        echo 'success';
    }

    public function getDate($date)
    {
        $year = '20'.substr($date,0,2);
        $month =  substr($date,2,2);
        $day =  substr($date,4,2);
        $hour =  substr($date,6,2);
        $minute =  substr($date,8,2);
        $s =  substr($date,10,2);
        $newDate = $year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$s;
        return $newDate;
    }
}
